package com.third.bank.controller;

import com.third.bank.common.IdCardLoginCommon;
import com.third.bank.common.PhoneLoginCommon;
import com.third.bank.common.UserCommon;
import com.third.bank.entity.User;
import com.third.bank.service.UserService;
import com.third.bank.util.BaseResponse;
import com.third.bank.util.JwtUtil;
import com.third.bank.util.RandomBankCardUtils;
import com.third.bank.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * 客户Controller
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册新用户
     * @param userCommon
     * @return
     */
    @ApiOperation(value = "注册客户接口",notes = "注册新客户接口的说明")
    @PostMapping("/register")
    public BaseResponse registerUser(@RequestBody UserCommon userCommon){
        BaseResponse baseResponse = userService.registerUser(userCommon);
        return baseResponse;
    }

    /**
     * 手机号验证码登录
     * @return
     */
    @ApiOperation(value = "手机号登录接口",notes = "手机号登录接口的说明")
    @PostMapping("/phone/login")
    public BaseResponse phoneLogin(@RequestBody PhoneLoginCommon phoneLoginCommon){
        BaseResponse baseResponse = userService.phoneLogin(phoneLoginCommon);
        return baseResponse;
    }

    /**
     * 身份证密码登录
     * @return
     */
    @ApiOperation(value = "身份证密码登录接口",notes = "身份证密码登录接口的说明")
    @PostMapping("/idCard/login")
    public BaseResponse phoneLogin(@RequestBody IdCardLoginCommon idCardLoginCommon){
        BaseResponse baseResponse = userService.idCardLogin(idCardLoginCommon);
        return baseResponse;
    }

    /**
     * 第一次登录设置网银密码
     * @return
     */
    @ApiOperation(value = "身份证密码登录接口",notes = "身份证密码登录接口的说明")
    @PutMapping("/updateLgPassword")
    public BaseResponse phoneLogin(String lgPassword, HttpServletRequest request){
        String token = request.getHeader("Authorization");

        System.out.println(token);

        String idCard = JwtUtil.parse(token);
        BaseResponse baseResponse = userService.updateLgPassword(token,idCard,lgPassword);
        return baseResponse;
    }

    /**
     * 用户登出
     * @param request
     * @return
     */
    @ApiOperation(value = "用户登出接口",notes = "用户登出接口的说明")
    @PostMapping("/logout")
    public BaseResponse logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");

        BaseResponse logout = userService.logout(token);
        return logout;
    }
}
